package com.crm.service.productionService;

import com.alibaba.druid.util.StringUtils;
import com.crm.dao.productionDAO.ProductionCategoryDAO;
import com.crm.dao.productionDAO.ProductionDetilsDAO;
import com.crm.pojo.ProductionDetail;
import com.crm.pojo.Productioncategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ProductionCategoryServiceImpl implements ProductionCategoryService {
    @Autowired
    private ProductionCategoryDAO productionCategoryDAO;
    @Autowired
    private ProductionDetilsDAO productionDetilsDAO;

    //分页查询所有的产品分类
    public List<Productioncategory> allProductionCategoryPaged(int curpage, int rows){
        PageRequest pageRequest = new PageRequest(curpage-1,rows);
        List<Productioncategory> productioncategories = productionCategoryDAO.queryByPage(pageRequest);
        for (Productioncategory p:productioncategories) {
            if(p.getPcpid()==null){

            }else{
                if(p.getPcpid()==0){
                    p.setPcpname("已是一级分类");
                }else{
                    p.setPcpname(productionCategoryDAO.findOne(p.getPcpid()).getPcname());
                }
            }

        }
        return productioncategories;
    }
    //查询总记录数
    public Long queryCount(){
        return productionCategoryDAO.count();
    }
    //添加分类
    public void addCategory(Productioncategory productioncategory){
        productionCategoryDAO.save(productioncategory);
    }
    //删除分类（逻辑删除）
    public void deleteCategory(Productioncategory productioncategory){
        productioncategory.setProductioncategoryIsDel("已删除");
        productionCategoryDAO.save(productioncategory);
    }
    //修改分类
    public void updateCategory(Productioncategory productioncategory){
        productionCategoryDAO.save(productioncategory);
    }
    //模糊查询（根据pcid或者pcname）
    public List<Productioncategory> queryBypcidOrpcnameMh(String q){
        return productionCategoryDAO.queryBypcidOrpcnameMh("%"+q+"%");
    }
    //模糊查询（根据pcid或者pcname）但是不能自己
    public List<Productioncategory> queryBypcidOrpcnameMh(String q, String noteq){
        return productionCategoryDAO.queryBypcidOrpcnameMh2("%"+q+"%",noteq);
    }
    //根据id查询分类
    public Productioncategory queryOneById(Long pid){
        return productionCategoryDAO.findOne(pid);
    }
    //根据pcpid查询分类
    public List<Productioncategory> querySomeByPcpid(Long pcpid){
        return productionCategoryDAO.findByPcpid(pcpid);
    }
    //根据分类id查询产品
    public List<ProductionDetail> queryPdsByPcid(Long pcid){
        return productionDetilsDAO.queryPdsByPcategory(pcid);
    }

    //高级查询（多条件查询）
    //根据任意条件查询产品分类信息，不填任意项则查询所有的产品分类
    @Override
    public List<Productioncategory> proChlq(Productioncategory productioncategory, int curpage, int pagesize) {
        Assert.notNull(productioncategory);
        PageRequest pageRequest = new PageRequest(curpage-1,pagesize);
        @SuppressWarnings("unchecked")
        Page<Productioncategory> result= productionCategoryDAO.findAll(new Specification<Productioncategory>() {
            @Override
            public Predicate toPredicate(Root<Productioncategory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //创建条件集合
                List<Predicate> list  = new ArrayList<>();
                //添加条件
                if(!StringUtils.isEmpty(productioncategory.getPcname())){
                    list.add(criteriaBuilder.like(root.get("pcname").as(String.class),"%"+productioncategory.getPcname()+"%"));
                }
                list.add(criteriaBuilder.equal(root.get("productioncategoryIsDel").as(String.class),"未删除"));
                if(productioncategory.getPcdepth()!=null){
                    list.add(criteriaBuilder.equal(root.get("pcdepth").as(Long.class),productioncategory.getPcdepth()));
                }
                if(productioncategory.getPcpid()!=null){
                    list.add(criteriaBuilder.equal(root.get("pcpid").as(Long.class),productioncategory.getPcpid()));
                }
                if(productioncategory.getPcpriority()!=null){
                    list.add(criteriaBuilder.equal(root.get("pcpriority").as(Long.class),productioncategory.getPcpriority()));
                }
                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        },pageRequest);
        List<Productioncategory> list = result.getContent();
        return list;
    }
    //高级查询对应条件下的总记录数


    @Override
    public Long proChlqCount(Productioncategory productioncategory) {
        Assert.notNull(productioncategory);
        @SuppressWarnings("unchecked")
        Long count = productionCategoryDAO.count(new Specification<Productioncategory>() {
            @Override
            public Predicate toPredicate(Root<Productioncategory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //创建条件集合
                List<Predicate> list  = new ArrayList<>();
                //添加条件
                if(!StringUtils.isEmpty(productioncategory.getPcname())){
                    list.add(criteriaBuilder.like(root.get("pcname").as(String.class),"%"+productioncategory.getPcname()+"%"));
                }
                list.add(criteriaBuilder.equal(root.get("productioncategoryIsDel").as(String.class),"未删除"));
                if(productioncategory.getPcdepth()!=null){
                    list.add(criteriaBuilder.equal(root.get("pcdepth").as(Long.class),productioncategory.getPcdepth()));
                }
                if(productioncategory.getPcpid()!=null){
                    list.add(criteriaBuilder.equal(root.get("pcpid").as(Long.class),productioncategory.getPcpid()));
                }
                if(productioncategory.getPcpriority()!=null){
                    list.add(criteriaBuilder.equal(root.get("pcpriority").as(Long.class),productioncategory.getPcpriority()));
                }
                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        });
        return count;
        }
    //查询最大的分类深度
    public int queryMaxD(){
        return productionCategoryDAO.queryMaxDepth();
    }
}
